package com.rycx.facefeatureclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.awt.*;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.time.LocalDateTime;

/**
 * D:\JDK17\IH\bin\jpackage.exe --input target --main-jar FacefeatureClient.jar --main-class org.springframework.boot.loader.launch.JarLauncher --name FaceApp --app-version 1.0 --type exe --dest dist --win-shortcut --win-menu --icon icon.ico
 */
@SpringBootApplication
public class FacefeatureClientApplication {

    private static void log(String message) {
        try {
            // 在用户目录下创建日志文件
            String userHome = System.getProperty("user.home");
            FileWriter fw = new FileWriter(userHome + "\\facefeature_debug.log", true);
            PrintWriter pw = new PrintWriter(fw);
            pw.println(LocalDateTime.now() + ": " + message);
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        log("应用开始启动...");

        try {
            log("正在启动Spring Boot应用...");
            SpringApplication.run(FacefeatureClientApplication.class, args);
            log("Spring Boot应用启动完成");

            // 等待应用启动完成后打开浏览器
            new Thread(() -> {
                try {
                    log("等待3秒后尝试打开浏览器...");
                    Thread.sleep(3000);

                    log("检查图形环境...");
                    boolean isHeadless = GraphicsEnvironment.isHeadless();
                    log("isHeadless: " + isHeadless);

                    String url = "http://localhost:8090/index.html";
                    log("尝试打开URL: " + url);

                    if (!isHeadless) {
                        log("使用Desktop.browse()方式...");
                        Desktop.getDesktop().browse(new URI(url));
                        log("Desktop.browse()调用完成");
                    } else {
                        log("使用cmd命令方式...");
                        Process process = Runtime.getRuntime().exec("cmd /c start " + url);
                        log("cmd命令执行完成，退出码: " + process.waitFor());
                    }
                } catch (Exception e) {
                    log("打开浏览器时发生异常: " + e.getMessage());
                    e.printStackTrace();

                    // 尝试另一种方式
                    try {
                        log("尝试使用rundll32方式...");
                        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler http://localhost:8090/index.html");
                        log("rundll32命令执行完成");
                    } catch (Exception e2) {
                        log("rundll32方式也失败: " + e2.getMessage());
                    }
                }
            }).start();

        } catch (Exception e) {
            log("应用启动失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}